#include <QMenu>
#include <QAction>
#include <QMap>
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "process_files/file_manager.h"
#include "data_files/track_data_table_model.h"
#include "data_files/track_data_table_model_02.h"
#include "forms/flight_track_stage.h"
#include "data_files/constant_variable.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , manager_(new File_manager(this))
    , track_data_model_(new Track_data_table_model(this))
//    , track_data_model_02(new Track_data_table_model_02(this))
    , track_stage_(new Flight_track_stage(this))
{
    ui->setupUi(this);

    init_widgets();
    init_connections();

    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init_widgets()
{
    // 设置控件
    file_menu_ = menuBar()->addMenu(tr("文件"));
    open_file_action_ = new QAction(tr("打开文件"), this);
    split_data_action_ = new QAction(tr("拆分数据"), this);
    exit_action_ = new QAction(tr("退出"), this);

    file_menu_->addAction(open_file_action_);
    file_menu_->addAction(split_data_action_);
    file_menu_->addSeparator();
    file_menu_->addAction(exit_action_);

    ui->spin_box_max_altitude_->setRange(0, 15000);
    ui->spin_box_max_altitude_->setValue(3000);
    ui->spin_box_min_altitude_->setRange(0, 15000);
    ui->spin_box_min_altitude_->setValue(1500);

    // 将模型，视图绑定
    ui->tableView->setModel(track_data_model_);
//    ui->tableView->setModel(track_data_model_02);
    ui->scroll_area_->setWidget(track_stage_);
}

void MainWindow::init_connections()
{
    connect(open_file_action_, &QAction::triggered,
            manager_, &File_manager::si_read_data_from_file);

    connect(split_data_action_, &QAction::triggered,
            manager_, &File_manager::si_open_file_to_split_data);

    connect(exit_action_, &QAction::triggered,
            manager_, &File_manager::si_exit_);

    connect(ui->button_start_, &QPushButton::clicked,
            this, &MainWindow::collect_filters);

    // 将处理后的数据发给模型，显示数据
    connect(manager_, &File_manager::si_send_track_to_model,
            track_data_model_, &Track_data_table_model::si_get_track_data);
}

void MainWindow::collect_filters()
{
    QMap<QString, QString> map;
    map.insert(SELECTED_SSR_CODE, ui->edit_ssr_code_->text().trimmed());
    map.insert(FLIGHT_NUMBER, ui->edit_flight_number_->text().trimmed());
    map.insert(MAX_LATATIUDE, QString::number(ui->spin_box_max_altitude_->value(), 10));
    map.insert(MIN_LATATIUDE, QString::number(ui->spin_box_min_altitude_->value(), 10));

    // 发送用户选择条件，后台处理
    emit manager_->si_fetch_filter_conditions(map);
//    emit track_data_model_02->si_get_filtered_flag(true);
}

